import api from "../../utils/api_util";
import baseUtil from "../../utils/base_util";
import tips from "../../utils/tip";

var WxParse = require('../../wxParse/wxParse.js');

const app = getApp();
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        TabCur: 0,
        shopDetailData: [],
        TabCurData: [{
            name: "商品详情",
            id: 0
        },
            {
                name: "评价",
                id: 1
            }
        ],
        rating: [{
            id: 0,
            scores: 5,
        }],
        //购物车总数
        shopCarNumber: '',
        //评论
        commentScore: '',
        pageNumber: 1,
        pageSize: 20,
        hasNext: true,
        commentList:[],
        scoreText:[
            {
                "name":"全部",
                "num":"",
                "level":'',
            },
            {
                "name":"好评",
                "num":"",
                "level":3,
            },
            {
                "name":"中评",
                "num":"",
                "level":2,
            },
            {
                "name":"差评",
                "num":"",
                "level":1,
            },
        ]
    },
    onShow: function () {
        this.getShopDetail();
        this.getShopCarNumber();
        this.getCommentScore();
    },
    //商品详情
    getShopDetail() {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.goosDetail, {
            method: 'GET',
            data: {
                // id: 6,
                id: baseUtil.getCurrentUrlParam('id'),
            },
            success: function (res) {
                WxParse.wxParse('shopContent', 'html', res.data.content, that, 5);
                that.setData({
                    shopDetailData: res.data
                })
            },
        })
    },

    jumpPage:function(e){

        let url = e.currentTarget.dataset.url;

        baseUtil.pageJump(url,true);

    },
    /**
     购物车
     */

    //购物车列表
    getShopCarNumber() {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.listShoppingCart, {
            method: 'GET',
            data: {
                pageSize: '',
                pageNumber: '',
            },
            success: function (res) {

                that.setData({
                    shopCarNumber: res.data.total
                })
            },
        })
    },
    //加入购物车
    addShopCarPost() {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.addShoppingCart, {
            method: 'POST',
            data: {
                goodsId: that.data.shopDetailData.id,
                storeId: that.data.shopDetailData.storeId,
                number: 1,
                price: that.data.shopDetailData.currentPrice,
                goodsAttrIds: '',
            },
            success: function (res) {
                // setTimeout(() => {
                //     tips.alert('添加成功，在购物车等你~')
                // },100);
                setTimeout(() => {
                    that.getShopCarNumber();

                }, 500);
            },
        })
    },
    tabSelect(e) {
        let that =this;
        console.log(e);
        if(e.currentTarget.dataset.id == 1){
            that.data.pageNumber = 1;
            that.getCommentList();
        }
        this.setData({
            TabCur: e.currentTarget.dataset.id,
            pageNumber: that.data.pageNumber,
            commentList: [],
            hasNext: true,
        })
    },

    /**
     * 评论
     */

    getCommentScore: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.scoreByGood, {
            method: 'GET',
            data: {
                // goodId: 6,
                goodId: baseUtil.getCurrentUrlParam('id'),
                v: "1.0.0"
            },
            success: function (res) {

                that.data.scoreText[0].num = res.data.zong;
                that.data.scoreText[1].num = res.data.hao;
                that.data.scoreText[2].num = res.data.zhong;
                that.data.scoreText[3].num = res.data.cha;

                that.setData({
                    scoreText: that.data.scoreText
                })
            },
        })
    },
    clickTabComment:function(e){
        let that=this;
        let level =e.currentTarget.dataset.level;
        that.data.pageNumber = 1;
        that.data.commentList = [];
        that.data.hasNext = true;
        that.getCommentList(level);
        that.setData({
            pageNumber: that.data.pageNumber,
            commentList: that.data.commentList,
            hasNext: that.data.hasNext,
        })
    },

    //评论列表
    getCommentList: function (level) {
        let that = this;

        api.wxRequest.apiRequest(that, api.urls.listByGood, {
            method: 'GET',
            data: {
                goodId: baseUtil.getCurrentUrlParam('id'),
                v: "1.0.0",
                pageNumber:that.data.pageNumber,
                pageSize:that.data.pageSize,
                level:level ? level : '',
            },
            success: function (res) {
                if (res.data.totalCount > (res.data.pageSize * res.data.currentPage)) {
                    that.data.hasNext = true;
                } else {
                    that.data.hasNext = false;
                    console.log('没有了')
                }

                if (res.data.rows && res.data.rows.length > 0) {
                    that.data.commentList = that.data.commentList.concat(res.data.rows);
                }

                console.log('订单列表数据:', that.data.statusType);

                that.setData({
                    commentList: that.data.commentList,
                    hasNext: that.data.hasNext,
                })
            },
        })
    },

    onReachBottom:function () {
        let that = this;

        if(this.data.TabCur == 1){
            console.log('翻页啊~~');
            if (that.data.hasNext) {
                that.data.pageNumber++;
                that.getCommentList();
            }
            that.setData({
                pageNumber: that.data.pageNumber,
            })
        }else {

            console.log('不管')
        }

    }

})